<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;

class poster extends AdminController
{
    

    public function index()
    {
        //$list = Db::name('poster')->select();
        $list = Db::name('poster')->paginate(3);
        return view('poster/index',[
            'list'=>$list
        ]);
    }

    public function add()
    {
         // $a = Db::name('poster')->select();

        //  $array = array();
        //  foreach($a as $val){
        //
        //      $array[]= $val['poster_name'];
        //  }
        // var_dump($array);
        //
        // $jsonName = json_encode($array);
        // var_dump($jsonName);
        // die;
         return view('poster/add');
    }

    //图片上传的ajax操作
    public function server()
    {
        $name = isset($_POST['name'])? $_POST['name'] : '';
        $gender = isset($_POST['gender'])? $_POST['gender'] : '';

        $filename = md5(rand()).time().substr($_FILES['photo']['name'], strrpos($_FILES['photo']['name'],'.'));

        $response = array();

        if(move_uploaded_file($_FILES['photo']['tmp_name'], './static/posterpic/'.$filename)){
            $response['isSuccess'] = true;
            $response['name'] = $name;
            $response['gender'] = $gender;
            $response['photo'] = $filename;

        }else{
            $response['isSuccess'] = false;
        }

        echo json_encode($response);
    }



    public function save(Request $request)
    {
        
        $p = $request->post();
        // $result = Db::name('poster')->where('poster_name','=',$p['poster_name'])->find();
        // if($result < 0)
        // {
            $data = [
                'poster_name' => $p['poster_name'],
                'poster_pic' => $p['picname']
            ];
            $result = Db::name('poster')->data($data)->insert();
            // if ($result) {
            //     return $this->success('添加成功(((((((((((っ･ω･)っ Σ(σ｀･ω･´)σ 飞天了！', url('admin/poster/index'));
            // }else{
            //     return $this->error('添加失败(((((((((((っ･ω･)っ Σ(σ｀･ω･´)σ 坠机了！', url('admin/poster/add'));

            // }
            return $this->redirect('admin/poster/index');
        // }else{
        //
        //     return "<script>alert('广告名已存在,无法上传');window.history.back();</script>";
        // }


    }

   
    function nn(Request $request)
    {
        $p = $request->post();
        $a = Db::name('poster')->select();

         $array = array();
         foreach($a as $val){

             $array[]= $val['poster_name'];
         }

        // var_dump($array);
        //
        // var_dump($p['name']);
        // die;
        if (in_array($p['name'],$array)) {
            $info['status'] = false;
        } else {
            $info['status'] = true;
        }
        // $info['status'] = true;
        return json($info);
    }


    public function edit($id)
    {

        $data = Db::name('poster')->find($id);
        return view('poster/edit',[
            'data'=> $data
        ]);
    }

   


    public function update(Request $request, $id)
    {

        $p = $request->post();
        //判断是否上传了新的图片
        if(!empty($p['poster_pic'])){
            //上传新图片删除旧图片
            $data = Db::name('poster')->find($id);
            if(!empty($data['poster_pic'])){
                $url = './static/posterpic/'.$data['poster_pic'];
                // File->unlink($url);
                unlink($url);
            }
            $data = [
                'poster_name' => $p['poster_name'],
                'poster_pic' => $p['poster_pic']

            ];

        }else{

            $data = [

                'poster_name' => $p['poster_name']

            ];

        }
    
        $result = Db::name('poster')->where('poster_id',$id)->update($data);
        // if ($result) {
        //     return $this->success('修改文章成功(((((((((((っ･ω･)っ Σ(σ｀･ω･´)σ 飞天了！', url('admin/poster/index'));
        // }else{
        //     return $this->error('修改文章失败(((((((((((っ･ω･)っ Σ(σ｀･ω･´)σ 坠机了！', url('admin/poster/add'));

        // }
        return $this->redirect('admin/poster/index');
    }

    public function delete()
    {
        $data = Db::name('poster')->find($_POST['posterid']);
        
        if(!empty($data['poster_pic'])){
            $url = './static/posterpic/'.$data['poster_pic'];
            unlink($url);
        }

        $result = Db::name('poster')->delete($_POST['posterid']);

        if ($result > 0) {
            $info['status'] = true;
        } else {
            $info['status'] = false;
        }
        // $info['status'] = true;
        return json($info);
    }
}
